import java.util.List;

/**
 * Created by loso on 2017/3/19.
 */
public class _14_LongestCommonPrefix {
//返回最长的公共字符串头
    public String longestCommonPrefix(String[] strs) {
        if (strs.length == 0){
            return "";
        }
            int len = Integer.MAX_VALUE;
        for (int i = 0; i < strs.length; i++) {
            len = len<strs[i].length()?len:strs[i].length();
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < len; i++) {
            for (int j = 1; j < strs.length; j++) {
                if (strs[j].charAt(i) !=strs[j-1].charAt(i)){
                    return sb.toString();
                }
            }
            sb.append(strs[0].charAt(i));
        }

        return sb.toString();
    }

    //如果要是有公共的字头的话就一定是有str[0]的，然后就用substring来截取字串。

    public String longestCommonPrefixBigBoss(String[] strs) {
        if(strs == null || strs.length == 0)    return "";
        String pre = strs[0];
        int i = 1;
        while(i < strs.length){
            while(strs[i].indexOf(pre) != 0)
                pre = pre.substring(0,pre.length()-1);
            i++;
        }
        return pre;
    }
}
